package model;

import helper.UserType;
import interfas.UserInterface;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databeans.User;

public class UserDAO implements UserInterface {
	private BeanFactory<User> factory;

	public UserDAO() throws DAOException {
		try {
			// Get a BeanTable.
			BeanTable<User> userTable = BeanTable.getInstance(User.class,"user");
			if (!userTable.exists()) userTable.create("userid");
			// Long running web application need to clean up idle database connections.
			// So we can tell each BeanTable to clean them up.  (You would only notice
			// a problem after leaving your web app running for several hours.)
			userTable.setIdleConnectionCleanup(true);
			// Get a BeanFactory which the actions will use to read and write rows of the "user" table
			factory = userTable.getFactory();
			
			// Make an admin account, if it doesn't already exist
			Transaction.begin();
			if (factory.lookup("admin") == null) {
				User admin = factory.create("admin");
				admin.setPassword("admin");
				admin.setType(UserType.ADMIN);
			}
			Transaction.commit();
			
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

	public void create(User user) throws DAOException {
		try {
			Transaction.begin();
			User dbUser = factory.create(user.getUserid());
			factory.copyInto(user,dbUser);
			Transaction.commit();
		} catch (DuplicateKeyException e) {
			throw new DAOException("registered");
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

	public User lookup(String Userid) throws DAOException{
		try {
			return factory.lookup(Userid);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	protected BeanFactory<User> getFactory(){ 
		return factory; 
	}

	public void updateUser(User user) throws DAOException {
		try {
			Transaction.begin();
			User dbUser = factory.lookup(user.getUserid());
			if (dbUser == null){
				throw new DAOException("User "+user.getUserid()+" no longer exists");
			}
			dbUser.setType(user.getType());
			dbUser.setPassword(user.getPassword());
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public void delete(String userid) throws DAOException {
		try {
			Transaction.begin();
			User dbUser = lookup(userid);
			if (dbUser == null)
				throw new DAOException("User with userid " + userid + " doesn't exist");
			factory.delete(userid);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}

	public int size() throws DAOException {
		try {
			return factory.getBeanCount();
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	@Override
	public User[] getUsers() throws DAOException {
		// TODO Auto-generated method stub
		try {
			User[] users = factory.match();
			return users;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
}
